package com.example.clxy.service.impl;

import com.example.clxy.dao.SchoolMapper;
import com.example.clxy.entity.School;
import com.example.clxy.result.PageResult;
import com.example.clxy.result.WebRestResult;
import com.example.clxy.result.business.PageResult.SchoolPageResult;
import com.example.clxy.service.SchoolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author Hsieh
 * @title: SchoolServiceImpl
 * @projectName clxy
 * @description:
 * @date 2020/8/1213:50
 */
@Service
public class SchoolServiceImpl implements SchoolService {

    @Autowired
    SchoolMapper schoolMapper;
    @Override
    public WebRestResult addSchool(School school) {
        WebRestResult result = new WebRestResult();
        school.setCreatedTime(new Date());
        school.setUpdatedTime(new Date());
        school.setFlag("0");
        if (schoolMapper.insert(school)==1){
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("添加成功！");
        }else {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("添加失败！");
        }
        return result;
    }

    @Override
    public WebRestResult deleteSchool(String schoolid) {
        WebRestResult result=new WebRestResult();
        if (schoolMapper.deleteByid(schoolid)==1){
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("删除成功！");
        }else {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("删除失败！");
        }
        return  result;
    }

    @Override
    public WebRestResult updateSchool(School school) {
        WebRestResult result=new WebRestResult();
        school.setUpdatedTime(new Date());
        if (schoolMapper.updateByPrimaryKey(school)==1){
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("修改成功！");
        }else {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("修改失败！");
        }
        return result;
    }

    @Override
    public PageResult<SchoolPageResult> getSchoolbyname(String schoolname, int page, int pagesize) {
        PageResult<SchoolPageResult> result = new PageResult<>();
        int begin = (page - 1) * pagesize;
        int end = pagesize;
        List<SchoolPageResult> list =schoolMapper.getSchoolbyname(schoolname, begin, end, pagesize);
        if (list.size() > 0) {
            result.setTotal(list.get(0).getCnt());
            result.setPageCount(list.get(0).getPage());
            result.setRows(list);
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("获取列表成功！");
        } else {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("获取列表失败！");
        }
        return result;
    }

    @Override
    public PageResult<SchoolPageResult> getSchoolbyid(String schoolid, int page, int pagesize) {
        PageResult<SchoolPageResult> result = new PageResult<>();
        int begin = (page - 1) * pagesize;
        int end = pagesize;
        List<SchoolPageResult> list =schoolMapper.getSchoolbyid (schoolid, begin, end, pagesize);
        if (list.size() > 0) {
            result.setTotal(list.get(0).getCnt());
            result.setPageCount(list.get(0).getPage());
            result.setRows(list);
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("获取列表成功！");
        } else {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("获取列表失败！");
        }
        return result;
    }
}
